<?php
/*
 * Created by Michael
 */
class AkMaster_ReportController extends Controller
{
	public $mainMenu = "Master Akademik";
	public $subMenu = "";
	public $title = "";
	public $module = "akademik";
	public $menuId = "master_tahunajaran";
	private $department_id='';
	
	public function init(){
		
		$this->department_id= Yii::app()->request->getQuery('department_id','');
		
	}
	
	public function actionIndex()
	{
		//echo "Testing";
	}
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		
		return true;
	}
	public function actionDepartment(){
		$this->subMenu = "Report Department";
		$output= Yii::app()->request->getQuery('output','web');
		$this->layout ="//layouts/ly_blank";
		$data = Yii::app()->db->createCommand()
		->select("name, description, 
				(case when is_active='1' then 'Aktif' else 'Non Aktif' end) as is_active")
		->from('m_department')
		->queryAll();
		if($output=='web'){
			$this->render('department',array('data' => $data));
		}
		if($output=='excel'){
			$this->OutputExcel($data,'department.xls','department.xls');
		}
		
	}
	public function actionTingkatan(){
		$this->subMenu = "Report Tingkatan";
		$output= Yii::app()->request->getQuery('output','web');
		
		$this->layout ="//layouts/ly_blank";
		$data = Yii::app()->db->createCommand()
		->select("name, description, (case when is_active='1' then 'Aktif' else 'Non Aktif' end) as is_active")
		->from('m_tingkatan')
		->where('department_id=:department_id', array(':department_id'=>$this->department_id))
		->queryAll();
	
		if($output=='web'){
			$this->render('tingkatan',array('data' => $data));
		}
		if($output=='excel'){
			$this->OutputExcel($data,'tingkatan.xls','tingkatan.xls');
		}
	}
	public function actionSemester(){
		$this->subMenu = "Report Semester";
		$output= Yii::app()->request->getQuery('output','web');
	
		$this->layout ="//layouts/ly_blank";
		$data = Yii::app()->db->createCommand()
		->select("name, description, (case when is_active='1' then 'Aktif' else 'Non Aktif' end) as is_active")
		->from('m_semester')
		->where('department_id=:department_id', array(':department_id'=>$this->department_id))
		->queryAll();
	
		if($output=='web'){
			$this->render('semester',array('data' => $data));
		}
		if($output=='excel'){
			$this->OutputExcel($data,'semester.xls','semester.xls');
		}
	}
	
	public function actionKelas(){
		$this->subMenu = "Report Kelas";
		$tingkatan_id= Yii::app()->request->getQuery('tingkatan_id','1');
		$output= Yii::app()->request->getQuery('output','web');
		
		$this->layout ="//layouts/ly_blank";
		$data = Yii::app()->db->createCommand()
		->select("name, keterangan as description, (case when is_active='1' then 'Aktif' else 'Non Aktif' end) as is_active")
		->from('m_kelas')
		->where('department_id=:department_id and tingkatan_id=:tingkatan_id', array(':department_id'=>$this->department_id , ':tingkatan_id'=>$tingkatan_id))
		->queryAll();
	
		if($output=='web'){
			$this->render('kelas',array('data' => $data));
		}
		if($output=='excel'){
			$this->OutputExcel($data,'kelas.xls','kelas.xls');
		}
	}
	private function OutputExcel($data, $filetemplate, $filehasil="hasil.xls")
	{
		$filetemplate = dirname(__FILE__) . DIRECTORY_SEPARATOR . "..". DIRECTORY_SEPARATOR ."exceltemplate". DIRECTORY_SEPARATOR ."akMaster_Report". DIRECTORY_SEPARATOR .$filetemplate;
		
		$department_id= Yii::app()->request->getQuery('department_id','1');
		if(file_exists($filetemplate)){
			// get a reference to the path of PHPExcel classes
			$phpExcelPath = Yii::getPathOfAlias('ext.phpexcel.Classes');
			require_once $phpExcelPath. DIRECTORY_SEPARATOR ."PHPExcel". DIRECTORY_SEPARATOR ."IOFactory.php";
				
			$inputFileType = PHPExcel_IOFactory::identify($filetemplate);
			$objPHPExcel = PHPExcel_IOFactory::load($filetemplate);
			$objPHPExcel->setActiveSheetIndex(0);
			
			$start_row=3;
			$row=$start_row;
			$col=0;
			$cols = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P');
			foreach($data as $datarow){
				$col=0;
				$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
				foreach($datarow as $key => $value){
					$objPHPExcel->getActiveSheet()->SetCellValue($cols[$col++].$row, $value);
				}
				$row++;
			}
			$end_col = $col-1;
			//$objPHPExcel->getActiveSheet()->duplicateStyle($objPHPExcel->getActiveSheet()
			//->getStyle('A'.$row), 'A'.$start_row.':'. $cols[$end_col].($row-1));
			//Hapus Baris awal
			$objPHPExcel->getActiveSheet()->removeRow($start_row-1,1);
			
			for($cx=0;$cx<=$end_col;$cx++){
				$objPHPExcel->getActiveSheet()->getColumnDimension($cols[$cx])->setAutoSize(true);
			}
			
			//$objPHPExcel->getActiveSheet()->setTitle('Department');
			$this->HeaderExcel($filehasil);
						
			//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
			$objWriter->save('php://output');
			exit;
				
				
		}
		
	}
		
	private function HeaderExcel($filehasil){
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filehasil.'"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		
		// If you're serving to IE over SSL, then the following may be needed
		header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
		header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header ('Pragma: public'); // HTTP/1.0
		
		
	}
}
